﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Yang.DataBase;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string ProductType = "";                //成果类型
        //int language = 0; //语言类型（中文1、英文2、全部0）
        int PageNumber = 1;




        #region 查询条件设置
        //示例： queryString = "***.aspx?DATATYPE=1&AuthorNameCN=王纪华&AuthorNameEN=Wang Jihua&StartYear=2006&EndYear=2010&FundNumber=2010003901&TEMP=0.131244"

        //string queryString = Utility.InitChineseUrl(Request.Url);
        string queryString = Request.Url.ToString();
        queryString = queryString.Split('?')[1]; //得到?以后部分的字段
        ProductType = queryString.Split('&')[0].Trim();//得到第一个queryString，也就是成果类型字符串
        ProductType = ProductType.Split('=')[1].Trim(); //得到成果类型的值

        //string strLanguage = queryString.Split('&')[1].Trim();
        //strLanguage = strLanguage.Split('=')[1].Trim();
        //language = int.Parse(strLanguage);

        string strPageNumber = queryString.Split('&')[1].Trim();
        strPageNumber = strPageNumber.Split('=')[1].Trim();
        PageNumber = int.Parse(strPageNumber);

        //System .IO .TextWriter txt = new System .IO .TextWriter;


        queryString = queryString.Remove(queryString.LastIndexOf('&') - 1); //移除最后的TEMP字段
        //if (queryString.IndexOf("LANGUAGE=") > -1)
        //    queryString = queryString.Remove(0, queryString.IndexOf("LANGUAGE=") + 10);
        if (queryString.IndexOf("QUERYSTRING=") > -1)
            queryString = queryString.Remove(0, queryString.IndexOf("QUERYSTRING=") + 12);  //移除QUERYSTRING前面的字段
        else
            queryString = "";
        if (queryString.Length > 0)
        {
            queryString = queryString.Replace("=", " LIKE '%");
            queryString = queryString.Replace(",", "%',");
            queryString = queryString.Replace(",", " AND ");
            queryString = queryString + "%'";

            if (queryString.IndexOf("开始年份") > -1)
            {
                queryString = queryString.Substring(0, queryString.IndexOf("开始年份") + 5) + " >= " +
                                queryString.Substring(queryString.IndexOf("开始年份") + 12, 4) +
                                queryString.Substring(queryString.IndexOf("开始年份") + 18);
                
            }
            if (queryString.IndexOf("结束年份") > -1)
            {
                queryString = queryString.Substring(0, queryString.IndexOf("结束年份") + 5) + " <= " +
                                queryString.Substring(queryString.IndexOf("结束年份") + 12, 4) +
                                queryString.Substring(queryString.IndexOf("结束年份") + 18);
            }

            if (ProductType == "3")
            {
                queryString = queryString.Replace("中文作者名", "中文发明人");
                queryString = queryString.Replace("英文作者名", "英文发明人"); 
                queryString = queryString.Replace("开始年份", "申请日期"); 
                queryString = queryString.Replace("结束年份", "申请日期");
            }
            else if (ProductType == "4")
            {
                queryString = queryString.Replace("中文作者名", "完成人");
                queryString = queryString.Replace("英文作者名", "完成人");
                queryString = queryString.Replace("开始年份", "日期");
                queryString = queryString.Replace("结束年份", "日期");
            }
            else if (ProductType == "5")
            {
                queryString = queryString.Replace("开始年份", "颁发日期");
                queryString = queryString.Replace("结束年份", "颁发日期");
            }
            else if (ProductType == "6" || ProductType == "7")
            {
                queryString = queryString.Replace("开始年份", "出版日期");
                queryString = queryString.Replace("结束年份", "出版日期");
            }
            else if (ProductType == "8")
            {
                queryString = queryString.Replace("开始年份", "答辩日期");
                queryString = queryString.Replace("结束年份", "答辩日期");
            }
            else
            {
                queryString = queryString.Replace("开始年份", "发表日期");
                queryString = queryString.Replace("结束年份", "发表日期");
            }
        }



        #endregion

        string SelectedColumn = "";
        #region 查询字段设置
        switch (ProductType)
        {
            case "1"://"驾驶员":
                SelectedColumn = "编号,驾驶员姓名,联系电话,联系地址,驾驶证号,档案号,准驾机型,驾驶农机,备注";
                break;
            case "2"://"农机":
                SelectedColumn = "编号,牌照号,所有人姓名,所属单位,机车类型,驾驶员编号,驾驶员姓名,备注";
                break;
            case "3"://"农具":
                SelectedColumn = "编号,农具编码,农具种类名称,所有人姓名,所属单位,农机编号,农机牌照号,备注";
                break;
            case "4"://"农具种类":
                SelectedColumn = "编号,农具种类名称,农具编码,幅宽,备注";
                break;
            case "5"://"作业类型":
                SelectedColumn = "编号,作业名称,技术要求,年度,牵引农具编码,牵引农具,折合系数,金额,附加说明,备注";
                break;

            default:
                break;

        }
        //}
        #endregion

        string[] selectedColumnArray = SelectedColumn.Split(',');

        #region 查询数据结果

        ProductType = Utility.ProductId2Name(ProductType);//得到成果类型的中文名称，也就是数据库中的表名称

            string resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
            resultXml += "<ResultList>" + "\n";
            if (queryString.Length == 0)
            {
                queryString = "select " + SelectedColumn + " from " + ProductType + " order by 编号";
            }
            else
            {
                queryString = "select " + SelectedColumn + " from " + ProductType + " where " + queryString + " order by 编号";
            }

            DataSet ds = Database.Query(queryString);

            DataTable dt = ds.Tables[0];

            int maxNumber = 0;
            //int minNumber = 0;

            if (dt.Rows.Count > 0)
            {
                if (PageNumber == 0)//当输入参数PageNumber=0，则表示保存查询结果至excel表格。
                {
                    //ExcelWriter writer = new ExcelWriter();

                    //string defaultPath = Server.MapPath("./") + "data\\";

                    //string fileName = ProductType
                    //                    + System.DateTime.Now.Year.ToString() + "-"
                    //                    + System.DateTime.Now.Month.ToString() + "-"
                    //                    + System.DateTime.Now.Day.ToString() + "-"
                    //                    + System.DateTime.Now.Hour.ToString() + "-"
                    //                    + System.DateTime.Now.Minute.ToString() + "-"
                    //                    + System.DateTime.Now.Second.ToString()
                    //                    + ".xls";
                    //string fullName = defaultPath + fileName;

                    //bool bWrite = writer.SaveExcel(dt, fullName);
                    //resultXml += "<Pager  Number = '";
                    //resultXml += bWrite.ToString();
                    //resultXml += "' url = '" + fileName;
                    //resultXml += "' />" + "\n";
                }
                else
                {
                    resultXml += "<Pager  Number = '";
                    resultXml += Convert.ToString(dt.Rows.Count / 10 + 1);
                    resultXml += "' />" + "\n";
                    if (dt.Rows.Count > PageNumber * 10 - 1)
                    {
                        maxNumber = PageNumber * 10;
                    }
                    else
                    {
                        maxNumber = dt.Rows.Count;
                    }
                    if (dt.Rows.Count >= (PageNumber - 1) * 10)
                    {

                        for (int i = (PageNumber - 1) * 10; i < maxNumber; i++)
                        {
                            //resultXml += "<Result id = '" + dt.Rows[i][2].ToString().Trim() + "' name = '" + dt.Rows[i][3].ToString() + "' distid='" + dt.Rows[i][0].ToString().Trim() + "' />" + "\n";
                            resultXml += "<Result ";
                            for (int j = 0; j < selectedColumnArray.Length; j++)
                            {
                                resultXml += selectedColumnArray[j].ToString().Trim() + " = '" + Utility.ReplaceXMLESC(dt.Rows[i][j].ToString().Trim()) + "' ";
                            }
                            resultXml += " />" + "\n";
                        }
                    }
                }

            }

            resultXml += "</ResultList>";
        
            Response.Write(resultXml);
        //}
# endregion
    }
}
